import os
from typing import Generator
import dashscope
from dotenv import load_dotenv
from dashscope.api_entities.dashscope_response import MultiModalConversationResponse
load_dotenv()
messages = [
    {
        "role": "user",
        "content": [
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"},
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"},
            {"image": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/rabbit.png"},
            {"text": "这些是什么?"}
        ]
    }
]
response = dashscope.MultiModalConversation.call(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY') or "",
    # 此处以qwen-vl-max为例，可按需更换模型名称。模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
    model='qwen-vl-max',
    messages=messages,
    # stream=True
)
print(type(response))
# print(response)
if isinstance(response, Generator):
    for i, chunk in enumerate(response):
        if (i == 0):
            import json
            json.dump(chunk, open('stream.output.json', 'w'))
        # print(chunk)
        import os
        os.system('cls' if os.name == 'nt' else 'clear')
        print(chunk["output"]["choices"][0]["message"]
              ["content"][0]["text"])
        pass
else:
    import json
    json.dump(response, open('full.output.json', 'w'))
    print(response["output"]["choices"][0]["message"]["content"][0]["text"])
